package com.wondream.plumelog.samples.utils;

import com.plumelog.core.TraceId;
import com.plumelog.core.util.IdWorker;
import com.wondream.plumelog.samples.constants.TraceConstants;
import org.slf4j.MDC;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 *
 * PlumeLog请求打点Utils
 *
 */
public class PlumeLogUtils {
    static IdWorker worker = new IdWorker(1, 1, 1);

    /**
     * 请求打点（为每次请求加上链路码）
     *
     * @param httpServletRequest
     */
    public static void setTraceIdInRequest(HttpServletRequest httpServletRequest) {
//        String traceId = httpServletRequest.getHeader(TraceConstants.TRACE_ID_HTTP_FIELD);
//        if (StringUtils.isEmpty(traceId)) {
//            TraceId.logTraceID.set(String.valueOf(worker.nextId()));
//        } else {
//            TraceId.logTraceID.set(traceId);
//        }
        TraceId.logTraceID.set(MDC.get(TraceConstants.TRACE_ID_MDC_FIELD));
    }
}
